package com.ylw.entity;

import com.ylw.inter.ReadTable;
import com.ylw.util.Constant;
import org.apache.log4j.Logger;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public abstract class Define implements Callable<String>,ReadTable {
    protected static final String SERVICE_NAME = "服务名称";
    protected static final String SERVICE_CODE = "ServiceCode";
    protected static final String REQUEST_PARAMETER = "请求入参";
    protected static final String RESPONSE_PARAMETER = "返回出参";
    protected static final String SERVICE_METHOD = "服务方式";
    protected static final String MAIN_USER = "主要使用者";
    protected static final String SERVICE_DESCRIPTION = "服务描述";
    private static final Logger logger = Logger.getLogger(Define.class);
    protected static final List<String> superParament = new ArrayList();  //键数组


    static {
        superParament.add(SERVICE_NAME);
        superParament.add(SERVICE_CODE);
        superParament.add(REQUEST_PARAMETER);
        superParament.add(RESPONSE_PARAMETER);
        superParament.add(SERVICE_METHOD);
        superParament.add(MAIN_USER);
        superParament.add(SERVICE_DESCRIPTION);

    }


    protected XWPFTable table;
    protected Map map = new HashMap();
    protected Service service;


    //构造时必须挂在service下面
    public Define(Service service, XWPFTable table) {
        this.table = table;
        this.service = service;
    }

    /**
     * 检查这个表是不是满足define条件
     *
     * @param table
     * @return
     */
    public abstract boolean check(XWPFTable table);

    /**
     * 每个子类实现自己的方法从table中读取数据
     * 这个方法应该private权限
     *
     * @return
     */
    public abstract String read();


    /**
     * 通过当前cell读取key值进行判断，然后将value取出来，讲key-value放到map中
     *
     * @param row
     * @param cellPos
     * @param keyName
     */
    protected void readKeyValue(XWPFTableRow row, int cellPos, String keyName) {
        if(row.getCell(cellPos)==null){
            return;
        }
        String key = row.getCell(cellPos).getText().trim();
        if (key == null || "".equalsIgnoreCase(key)) {
            return;
        }
        if (keyName.equalsIgnoreCase(key)) {
            if (map.get(keyName) == null) {
                String value = row.getCell(cellPos + 1).getText().trim();
                map.put(keyName, value);
                logger.info(cellPos+" "+keyName+" "+value);
            }
            else {
                logger.error(service.getName() + " " + keyName + Constant.IS_EXIST);
            }
        }
        else {
            logger.error(service.getName() + "  " + keyName + Constant.NOT_MATCH + key);
        }
    }


    /**
     * 开启线程，从table中读取数据到map中
     *
     * @return
     * @throws Exception
     */
    @Override
    public String call() throws Exception {
        return read();
    }

    public Map getMap() {
        return map;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"table\":").append(table);
        sb.append(",\"map\":").append(map);
        sb.append(",\"service\":").append(service);
        sb.append('}');
        return sb.toString();
    }
}
